package com.ljl.system.controller;


import com.ljl.basic.util.AjaxResult;
import com.ljl.basic.util.PageList;
import com.ljl.system.domain.DictionaryDetail;
import com.ljl.system.query.DictionaryDetailQuery;
import com.ljl.system.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/DictionaryDetail")
@Api(value = "字典的API",description="字典相关的CRUD功能")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    // 查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return dictionaryDetailService.findById(id);
    }

    // 查询所有
    @GetMapping()
    @ApiOperation(value = "查询全部" )
    public List<DictionaryDetail> queryAll(){
        return dictionaryDetailService.findAll();
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            dictionaryDetailService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }

    // 分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        return dictionaryDetailService.queryPage(dictionaryDetailQuery);
    }

    // 修改或添加
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryType){
        try {
            if (dictionaryType.getId() != null) {
                dictionaryDetailService.update(dictionaryType);
            } else {
                dictionaryDetailService.save(dictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }
    // 批量删除
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            dictionaryDetailService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }

}
